//
// Created by admin on 2022/10/11.
// Copyright (c) 2022 122674287@qq.com. All rights reserved.
//

#import "UIView+CYNoData.h"
#import "CYListViewController.h"
#import "CYBaseTableView.h"

@interface CYListViewController () <UITableViewDataSource, UITableViewDelegate, CYRefreshLoadMoreProtocol>
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, assign) NSInteger count;
@end

@implementation CYListViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view addSubview:self.tableView];
    self.tableView.frame = self.view.bounds;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    [self.tableView initDefaultListViewWithDelegate:self isLoadMore:YES];

#ifdef CY_BASE_KIT_DEBUG
    NSLog(@"custom Debug");
#else
    NSLog(@"custom Release");
#endif


#ifdef DEBUG
    NSLog(@"Debug");
#else
    NSLog(@"Release");
#endif
    // Do any additional setup after loading the view, typically from a nib.
    [self.tableView cy_startLoading];
    [self cy_listViewDidRefresh];

}

- (void)didReceiveMemoryWarning {
    // Dispose of any resources that can be recreated.
}

- (void)request:(BOOL)isRefresh {
//    [self.tableView cy_startLoading];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        if (isRefresh) {
//            self.count = 10;
//        } else {
//            self.count += 10;
//        }

        [self.tableView endAllAnimation];
        [self.tableView reloadData];
        [self.tableView cy_endLoading];//必须保证在reloadData后调用

    });
}

- (UITableView *)tableView {

    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
#ifdef __IPHONE_15_0
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
#endif
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];

        [_tableView cy_configEmptyView:[CYEmptyView cy_noDataEmptyNoAuto]];
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    cell.textLabel.text = [NSString stringWithFormat:@"===>  %ld", indexPath.row];
    return cell;
}

- (void)cy_listViewDidRefresh {
    [self request:YES];
}

- (void)cy_listViewLoadMore {
    [self request:NO];
}


@end
